'''
Created on Aug 1, 2011

@author: mark
'''
from data import get_cursor

class Workout(object):
    '''
    classdocs
    '''


    def __init__(self, name, date, description='', id=None):
        '''
        Constructor
        '''
        self.name = name
        self.date = date
        self.description = description
        self.id = id
        if self.id is None:
            curs = get_cursor()
            curs.execute("INSERT INTO workouts(name, date, description) VALUES ('%s', '%s', '%s')"
                           % (self.name, self.date.isoformat(), self.description))
            curs.connection.commit()
            curs.execute('SELECT LAST_INSERT_ID()')
            self.id = curs.fetchone()[0]
            curs.close()
        
        
        def save(self):
            curs = get_cursor()
            curs.execute("UPDATE workouts SET name = '%s', date='%s', description='%s' WHERE id = %d" 
                         % (self.name, self.date.isoformat(), self.description, self.id))
            curs.connection.commit()
            curs.close()